<?php namespace app\home\controller;

class Common{
    //动作
    public function __construct()
	{
	   //获取配置文件中的数据
	   $webSetData=$this->getWebSet();
//	   p($webSetData);
	   View::with('webSetData',$webSetData);
	   //获取分类信息
	   $cateData=$this->getCateData();
//	   p($cateData);
	   View::with('cateData',$cateData);
	   //获取分类数据
	   $allCateData=$this->getAllCate();
//	   p($allCateData);
	   View::with('allCateData',$allCateData);
	   //获取标签云
	   $tagData=$this->getTagData();
	   View::with('tagData',$tagData);
	   //友情链接
	   $friendlyLink=$this->getFriendlyLink();
//	   p($friendlyLink);
	   View::with('friendlyLink',$friendlyLink);
	   //最新文章
	   $newsData=$this->getNewsData();
//	   p($newsData);
	   View::with('newsData',$newsData);
	}
	//获取配置文件中的数据
	public function getWebSet(){
	   return Db::table('webset')->lists('name,value');
	}
   //获取分类信息
	public function getCateData(){
	   return Db::table('category')->where('pid',0)->limit(4)->get();
	}
   //获取分类数据
    public function getAllCate(){
	   $data=Db::table('category')->get();
	   foreach ($data as $k=>$v){
	      $data[$k]['count']=Db::table('article')->where('category_cid',$v['cid'])->count();
	   }
	   return $data;
	}
   //获取标签云
    public function getTagData(){
       $data=Db::table('tag')->get();
       foreach ($data as $k=>$v){
          $data[$k]['count']=Db::table('article_tag')->where('tag_tid',$v['tid'])->count();
	   }
       return $data;
	}
    //友情链接
    public function getFriendlyLink(){
       return Db::table('link')->get();
	}
   //最新文章
    public function getNewsData(){
       return Db::table('article')->orderBy('sendtime','DESC')->limit(2)->get();
	}
}
